home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: confusion between putchar and printf
- Date: 11 Mar 1996 14:13:59 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4i28j7INN65d@keats.ugrad.cs.ubc.ca>
- References: <4i1v2n$30o@news.azstarnet.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4i1v2n$30o@news.azstarnet.com>,
- Howard Salmon <captarm@azstarnet.com> wrote:
- >K & R (section 1.5) states that "putchar(c) prints the contents of the
- >integer variable c as a character, usually on the screen".
- ^^^^^^^^^^^^^^^^ ^^^^^^^^^
-
- >Here's a small program that I wrote testing putchar. Why doesn't it
- >compile?
- >
- >#include <stdio.h>
- ># define A "hello world!"
- >
- >main()
- >{
- > int c;
- > c = A;
- > putchar(A);
- >}
-
- Since when can you assign a string literal constant such as "hello world!" to
- an _integer_ variable?
-
- The C language has a concept of type. The values you assign to a variable
- must have a type that is compatible with the variable's type.
-
- Exactly what are you expecting this program to do? A single call to putchar is
- incapable of emitting a string no matter what argument you give to it. It
- prints a single character, exactly as K&R says. You can never get a single call
- to putchar to place a string on the screen. Try this:
-
- #include <stdio.h>
-
- int main()
- {
- char *p = "hello, world!\n";
-
- /* The string literal "hello, world\n" is stored in static memory.
- The variable p is a character pointer which initially holds the
- address of the character 'h'. */
-
- while (*p) /* '*p' refers to the character pointed at by p */
- /* we loop while this is not the zero character */
- putchar(*p++); /* print that character, & increment p */
- /* to point to the next character */
-
- return 0; /* all C programs need to return an exit status */
- }
- --
-
-